package ytdp.careagle.com.ytdp.activity;

import android.support.constraint.ConstraintLayout;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.umeng.analytics.dplus.UMADplus;

import butterknife.Bind;
import butterknife.OnClick;
import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.app.APP;
import ytdp.careagle.com.ytdp.app.manager.LoginManager;
import ytdp.careagle.com.ytdp.mvp.presenter.LogoutPresenter;
import ytdp.careagle.com.ytdp.mvp.view.ILogoutView;
import ytdp.careagle.com.ytdp.myview.MyEditText;
import ytdp.careagle.com.ytdp.myview.MyKeyboradView;
import ytdp.careagle.com.ytdp.utils.SPUtils;

public class LogoutActivity extends BaseActivity implements ILogoutView {

    @Bind(R.id.layout_content)
    View layoutContent;
    @Bind(R.id.tv_close)
    TextView tvClose;
    @Bind(R.id.et_username)
    MyEditText etUsername;
    @Bind(R.id.et_password)
    MyEditText etPassword;
    @Bind(R.id.tv_logout)
    TextView tvLogout;
    @Bind(R.id.myKeyboradView)
    MyKeyboradView myKeyboradView;
    @Bind(R.id.activity_main)
    ConstraintLayout activityMain;
    @Bind(R.id.tv_exit)
    TextView tvExit;
    private LogoutPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_logout;
    }

    @OnClick({R.id.tv_logout, R.id.tv_close, R.id.tv_exit, R.id.activity_main, R.id.layout_content})
    public void onClick(View view) {
        APP.getInstance().resetCountDown();
        switch (view.getId()) {
            case R.id.tv_logout:
                presenter.logout(getWContext().get());
                break;
            case R.id.tv_close:
                finish();
                break;
            case R.id.tv_exit:
                APP.getInstance().exit();
                break;
            case R.id.layout_content:
                myKeyboradView.closeKeyboard();
                break;
            case R.id.activity_main:
                if (myKeyboradView.isOpen()) {
                    myKeyboradView.closeKeyboard();
                } else {
                    finish();
                }
                break;
        }
    }


    @Override
    public void initView() {
        UMADplus.track(this, "登出页面", null);
        etUsername.setText(SPUtils.getUsername(this));
        etUsername.setFocusable(false);
        myKeyboradView.setEdittext(etPassword, MyKeyboradView.KEYBOARD_STATE_ENGLISH);
        //一开始不让软禁盘弹出
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        presenter = new LogoutPresenter(this);
    }

    @Override
    public void logoutSuccess() {
        LoginManager.logOut(this);
        getOperation().forward(LoginActivity.class);
        finish();
    }

    @Override
    public String getPassword() {
        return etPassword.getText().toString();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && myKeyboradView.getTranslationY() == 0) {
            myKeyboradView.closeKeyboard();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
